package com.twf.partnerMatching.model.vo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.gson.Gson;
import com.twf.partnerMatching.model.domain.User;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * @author TwfPlayer
 * @date 2023/10/8 15:45
 * @description: 脱敏用户VO
 */
@Data
public class UserClearVO implements Serializable {

    @JsonIgnore
    private static final Gson gson = new Gson();

    @TableField(exist = false)
    private static final long serialVersionUID = 6568612202423695728L;
    /**
     * 主键
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 昵称
     */
    private String username;

    /**
     * 用户账号
     */
    private String userAccount;

    /**
     * 用户角色
     */
    private Integer userRole;

    /**
     * 用户头像
     */
    private String avatarUrl;

    /**
     * 性别
     */
    private Integer gender;


    /**
     * 电话
     */
    private String phone;

    /**
     * 邮箱
     */
    private String email;

    /**
     * 用户状态:0-正常
     */
    private Integer userStatus;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 标签
     */
    private List<String> tags;


    public UserClearVO(User user) {
        this.id = user.getId();
        this.username = user.getUsername();
        this.userAccount = user.getUserAccount();
        this.userRole = user.getUserRole();
        this.avatarUrl = user.getAvatarUrl();
        this.gender = user.getGender();
        this.phone = setPhoneVO(user.getPhone());
        this.email = setEmailVO(user.getEmail());
        this.userStatus = getUserStatus();
        this.createTime = user.getCreateTime();
        this.tags = gson.fromJson(user.getTags(), List.class);
    }

    private String setPhoneVO(String phone) {
//        this.phone = phone;
        if (StringUtils.isBlank(phone)) {
            return "";
        }
        return StringUtils.left(phone, 3).concat(StringUtils
                .removeStart(StringUtils.leftPad(StringUtils.right(phone, 4), StringUtils.length(phone), "*"),
                        "***"));

    }

    private String setEmailVO(String email) {
//        this.email = email;
        if (StringUtils.isBlank(email)) {
            return "";
        }
        final int index = StringUtils.indexOf(email, "@");
        if (index <= 1) {
            return email;
        } else {
            return StringUtils.rightPad(StringUtils.left(email, 1), index, "*")
                    .concat(StringUtils.mid(email, index, StringUtils.length(email)));
        }
    }

    public Integer getUserStatus() {
        return userStatus;
    }

    public void setUserStatus(Integer userStatus) {
        this.userStatus = userStatus;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public List<String> getTags() {
        return tags;
    }

    public void setTags(String tags) {
        this.tags = gson.fromJson(tags, List.class);
    }
}